﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace LeitorNFe
{
    class Deserializer
    {
        // <summary>
        // Converts the String to UTF8 Byte array and is used in De serialization
        // </summary>
        // <param name="pXmlString"></param>
        // <returns></returns>
        private static Byte[] StringToUTF8ByteArray(string pXmlString )
        {
            return Encoding.GetEncoding("iso-8859-9").GetBytes(pXmlString);
        }

        //' <summary>
        //' Reconstruct an object from an XML string
        //' </summary>
        //' <param name="xml"></param>
        //' <returns></returns>
        public static T DeserializeObject<T>(string xml) 
        {
            try
            {
                XmlSerializer xs = new XmlSerializer(typeof(T));

                MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(xml));
                XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, null);

                return (T)xs.Deserialize(memoryStream);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
